package cn.cxq;

import java.util.ArrayList;

public class DataXUtil {

    public static String getJob(Table table, String username, String password, String url, String database) {
        StringBuilder builder = new StringBuilder();
        builder.append("{\n" +
                "\t\"job\": {\n" +
                "\t\t\"setting\": {\n" +
                "\t\t\t\"speed\": {\n" +
                "\t\t\t\t\"channel\": 3\n" +
                "\t\t\t},\n" +
                "\t\t\t\"errorLimit\": {\n" +
                "\t\t\t\t\"record\": 0,\n" +
                "\t\t\t\t\"percentage\": 0.02\n" +
                "\t\t\t}\n" +
                "\t\t},\n" +
                "\t\t\"content\": [{\n" +
                "\t\t\t\"reader\": {\n" +
                "\t\t\t\t\"name\": \"oraclereader\",\n" +
                "\t\t\t\t\"parameter\": {\n" +
                "\t\t\t\t\t\"username\": \""+ username +"\",\n" +
                "\t\t\t\t\t\"password\": \""+ password +"\",\n" +
                "\t\t\t\t\t\"where\": \"\",\n" +
                "\t\t\t\t\t\"connection\": [\n" +
                "\t\t\t\t\t{\n" +
                "\t\t\t\t\t\t\"querySql\": [\n" +
                "\t\t\t\t\t\t\t\"select ");

        ArrayList<Table.Column> columns = table.getColumns();

        for (int i = 0; i < columns.size(); i++) {
            builder.append(columns.get(i).columnName);
            if (i != columns.size() - 1) {
                builder.append(", ");
            }
        }

        builder.append(" from " + table.getTableName());

        builder.append(" \"\n" +
                "\t\t\t\t\t\t],\n" +
                "\t\t\t\t\t\t\"jdbcUrl\": [ \n" +
                "\t\t\t\t\t\t\t\""+ url +"\"\n" +
                "\t\t\t\t\t\t]\n" +
                "\t\t\t\t\t}\n" +
                "\t\t\t\t\t]\n" +
                "\t\t\t\t}\n" +
                "\t\t\t},\n" +
                "\t\t\t");

        builder.append("\"writer\": {\n" +
                "\t\t\t\t\"name\": \"hdfswriter\",\n" +
                "\t\t\t\t\"parameter\": {\n" +
                "\t\t\t\t\t\"hadoopConfig\": {\n" +
                "\t\t\t\t\t\t\"java.security.krb5.conf\": \"/opt/Bigdata/client/KrbClient/kerberos/var/krb5kdc/krb5.conf\",\n" +
                "\t\t\t\t\t\t\"dfs.nameservices\": \"hacluster\",\n" +
                "\t\t\t\t\t\t\"dfs.ha.namenodes.hacluster\": \"3,4\",\n" +
                "\t\t\t\t\t\t\"dfs.namenode.rpc-address.hacluster.3\": \"node-master2enxb.260c9765-192c-488e-ac44-606a709d6d2f.com:8020\",\n" +
                "\t\t\t\t\t\t\"dfs.namenode.rpc-address.hacluster.4\": \"node-master3igku.260c9765-192c-488e-ac44-606a709d6d2f.com:8020\",\n" +
                "\t\t\t\t\t\t\"dfs.client.failover.proxy.provider.hacluster\": \"org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider\",\n" +
                "\t\t\t\t\t\t\"hadoop.rpc.protection\":\"privacy\"\n" +
                "\t\t\t\t\t},\n" +
                "\t\t\t\t\t\"defaultFS\": \"hdfs://hacluster\",\n" +
                "\t\t\t\t\t\"haveKerberos\": \"true\",\n" +
                "\t\t\t\t\t\"kerberosKeytabFilePath\": \"/app/soft/client/conf/crcgas_dp/user.keytab\",\n" +
                "\t\t\t\t\t\"kerberosPrincipal\":\"dp_omm@HADOOP.COM\",\n" +
                "\t\t\t\t\t\"fileType\": \"orc\",\n" +
                "\t\t\t\t\t\"path\": \"/user/hive/warehouse/"+ database +".db/"+ table.getTableName() +"\",\n" +
                "\t\t\t\t\t\"fileName\": \"" + table.getTableName() + "\",\n" +
                "\t\t\t\t\t\"column\": [");

        for (int i = 0; i < columns.size(); i++) {
            Table.Column column = columns.get(i);
            builder.append("{\n" +
                    "\t\t\t\t\t\t\t\"name\": \""+ column.columnName +"\",\n" +
                    "\t\t\t\t\t\t\t\"type\": \"" + column.columnType + "\"\n" +
                    "\t\t\t\t\t\t}");
            if (i != columns.size() - 1) {
                builder.append(",\n" +
                        "\t\t\t\t\t\t");
            }
        }

        builder.append("],\n" +
                "\t\t\t\t\t\"writeMode\": \"append\", \n" +
                "\t\t\t\t\t\"fieldDelimiter\": \"\\u0001\", \n" +
                "\t\t\t\t\t\"compress\": \"snappy\"\n" +
                "\t\t\t\t}\n" +
                "\t\t\t}\n" +
                "\t\t}]\n" +
                "\t}\n" +
                "}\n");

        return builder.toString();
    }
}
